/* eslint-disable */

import { create } from 'dva-core';
import createLoading from 'dva-loading';
import { Provider, connect } from 'react-redux';
import { createLogger } from 'redux-logger';
import models, { merge, except } from './model';

let app;
let store;
let dispatch;

function createApp(opt) {
    opt.models = models;
    if (process.env.DEBUG) {
        opt.onAction = [createLogger()];
    }
    app = create(merge(opt));
    app.use(
        createLoading({
            except
        })
    );

    if (!global.registered) opt.models.forEach(model => app.model(model));
    global.registered = true;
    app.start();

    store = app._store;
    app.getStore = () => store;

    app.dispatch = dispatch = store.dispatch;
    app.start = container => () => (
        <Provider store={store}>{container}</Provider>
    );
    return app;
}

export default {
    createApp,
    getDispatch() {
        return app.dispatch;
    },
};
